package com.tongfusoft.deal.action;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import com.tongfusoft.web.core.action.BaseAction;

public class BaseWarpAction extends BaseAction{
	
	protected String getParameter (String key) {
		return this.getRequest().getParameter(key);
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	protected Map<String,Object> getParametersStartingWith(String prefix) {
		HttpServletRequest request = getRequest();
		Enumeration paramNames = request.getParameterNames();
		Map<String,Object> params = new TreeMap<String,Object>();
		if (prefix == null) {
			prefix = "";
		}
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				Object[] values = request.getParameterValues(paramName);
				if (values == null || values.length == 0) {// NOSONAR
				} else if (values.length > 1) {
					params.put(unprefixed, values);
				} else {
					params.put(unprefixed, values[0]);
				}
			}
		}
		return params;
	}
	
	
	public void showMap (Map<String,Object>param) {
		for (String key:param.keySet()) {
			Object value = param.get(key);
			System.out.println(key+"-->"+value);
		}
	}
	public String objToString (Object obj) {
		if (obj!=null) { 
			return obj.toString();
		}
		return "";
	}
	public String clobToString (Object obj)  {
		
		if (obj!=null&&obj instanceof Clob) {
			Clob clob = (Clob)obj;
			try {
				return clob.getSubString(1, (int) clob.length());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return "";
	}
}
